import 'package:simple_barcode_scanner/simple_barcode_scanner.dart' deferred as sbs;

import '../../../../core/utils/validation_functions.dart';
import '../../../../data/models/auth/user.dart';
import '../../../../widgets/app_bar/appbar_subtitle.dart';
import '../../../../widgets/app_bar/custom_app_bar.dart';
import '../../../../widgets/app_bar/outline_icon_button.dart';
import '../../../../widgets/custom_elevated_button.dart';
import '../../../../widgets/custom_text_form_field.dart';
import '../../../../widgets/gender_selection.dart';
import '../../../players/add_player_page/widgets/my_circle_avatar.dart';
import 'add_coach_page_cubit.dart';
import 'package:flutter/material.dart';
import 'package:syria_sports/core/app_export.dart';

class AddCoachPageScreen extends StatelessWidget {
  final User? coach;
  const AddCoachPageScreen({Key? key, this.coach}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    double height = MediaQuery.of(context).size.height;
    double width = MediaQuery.of(context).size.width;

    return Scaffold(
      appBar: _buildAppBar(context),
      body: BlocProvider<AddCoachPageCubit>(
        create: (context) => AddCoachPageCubit()..init(coach),
        child: BlocConsumer<AddCoachPageCubit, AddCoachPageState>(
          listener: (context, state) {
            // TODO: implement listener
          },
          builder: (context, state) {
            ImageConstant;
            var cubit = AddCoachPageCubit.get(context);

            return SizedBox(
              height: height,
              width: width,
              child: Column(
                children: [
                  Expanded(
                    child: Form(
                        key: cubit.formKey,
                        child: SingleChildScrollView(
                          physics: const BouncingScrollPhysics(),
                          child: Container(
                              width: double.maxFinite,
                              padding:
                              EdgeInsets.only(left: 16.h, top: 32.v, right: 16.h),
                              child: Column(
                                children: [
                                  MyCircleAvatar(
                                    width: 48,
                                    editMode: true,
                                    imageFile: cubit.image,
                                    imageURL:cubit.imageURL ,
                                    borderColor: Colors.grey,
                                    backgroundColor: Theme.of(context).colorScheme.onPrimaryContainer,
                                    onSelectedImage: (image){
                                      cubit.onChangeImage(image);
                                    },
                                  ),
                                  const SizedBox(height:16),
                                  // Full Name
                                  CustomTextFormField(
                                    controller: cubit.fullNameController,
                                    focusNode:cubit.fullNameFocusNode,
                                    onFieldSubmitted: (value){
                                      FocusScope.of(context).unfocus();
                                      FocusScope.of(context).requestFocus(cubit.barcodeFocusNode);
                                    },
                                    textInputAction: TextInputAction.next,
                                    hintText: "ادخل الاسم الكامل",
                                    title: 'الاسم الكامل',
                                    textInputType: TextInputType.name,
                                    prefix: Container(
                                      margin: EdgeInsets.fromLTRB(
                                        16.h, 12.v, 10.h, 12.v,
                                      ),
                                      child: CustomImageView(
                                        imagePath: ImageConstant.imgUser,
                                        height: 24.adaptSize,
                                        width: 24.adaptSize,
                                      ),
                                    ),
                                    prefixConstraints: BoxConstraints(maxHeight: 48.v),
                                    validator: (value) {
                                      if (value == null || value.isEmpty) {
                                        return 'هذه الخانة الزامية!';
                                      }
                                      return null;
                                    },
                                    contentPadding: EdgeInsets.only(
                                      top: 15.v,
                                      right: 30.h,
                                      bottom: 15.v,
                                    ),
                                  ),
                                  SizedBox(height: 10.v),
                                  // Gender Selection
                                  GenderSelection(
                                    value: cubit.gender,
                                    title: 'الجنس',
                                    onChanged:(value){
                                      cubit.gender = value;
                                      cubit.update();
                                    },
                                  ),
                                  /*SizedBox(height: 10.v),
                                  // Barcode
                                  CustomTextFormField(
                                      controller: cubit.barcodeController,
                                      focusNode:cubit.barcodeFocusNode,
                                      onFieldSubmitted: (value){
                                        FocusScope.of(context).unfocus();
                                        FocusScope.of(context).requestFocus(cubit.descriptionFocusNode);
                                      },
                                      hintText: "ادخل أو امسح الباركود من البطاقة",
                                      title: 'الباركود',
                                      textInputAction: TextInputAction.next,
                                      textInputType: TextInputType.visiblePassword,
                                      prefix: Container(
                                          margin: EdgeInsets.fromLTRB(
                                              16.h, 12.v, 10.h, 12.v),
                                          child: CustomImageView(
                                              imagePath: ImageConstant.icBarcode,
                                              color: Colors.grey,
                                              height: 24.adaptSize,
                                              width: 24.adaptSize)),
                                      suffix: IconButton(
                                        onPressed: ()async{
                                          await sbs.loadLibrary();
                                          var res = await Navigator.push(
                                              context,
                                              MaterialPageRoute(
                                                builder: (context) => sbs.SimpleBarcodeScannerPage(isShowFlashIcon: true,),
                                              ));
                                          if (res is String && res != "-1") {
                                            cubit.onScanBarcode(res);
                                          }
                                          */
                                  /*Navigator.push(context, MaterialPageRoute(builder: (context) => QRScreen(
                                            onBarcodeDetected: (barcode){
                                              cubit.onScanBarcode(barcode);
                                              Navigator.pop(context);
                                            },
                                          )));*/
                                  /*
                                        },
                                        icon: CustomImageView(
                                            imagePath: ImageConstant.icBarcodeScanner,
                                            height: 24.adaptSize,
                                            width: 24.adaptSize),
                                      ),
                                      prefixConstraints: BoxConstraints(maxHeight: 48.v),
                                      validator: (value) {
                                        if (value == null || value.isEmpty) {
                                          return 'هذه الخانة الزامية!';
                                        }
                                        return null;
                                      },
                                      obscureText: false,
                                      contentPadding: EdgeInsets.only(
                                          top: 15.v, right: 30.h, bottom: 15.v)),*/
                                  SizedBox(height: 10.v),
                                  // Description
                                  CustomTextFormField(
                                    controller: cubit.descriptionController,
                                    focusNode:cubit.descriptionFocusNode,
                                    onFieldSubmitted: (value){
                                      FocusScope.of(context).unfocus();
                                      FocusScope.of(context).requestFocus(cubit.birthdateFocusNode);
                                    },
                                    textInputAction: TextInputAction.newline,
                                    hintText: "ادخل وصف مختصر عن المدرب",
                                    title: 'الوصف',
                                    maxLines: 5,
                                    textInputType: TextInputType.multiline,
                                    contentPadding: EdgeInsets.only(
                                      top: 15.v,
                                      right: 30.h,
                                      bottom: 15.v,
                                    ),
                                  ),
                                  // Birthdate
                                  CustomTextFormField(
                                      controller: cubit.birthdateController,
                                      focusNode:cubit.birthdateFocusNode,
                                      onFieldSubmitted: (value){
                                        FocusScope.of(context).unfocus();
                                        FocusScope.of(context).requestFocus(cubit.passwordFocusNode);
                                      },
                                      hintText: "ادخل تاريخ الميلاد (31-12-2000)",
                                      title: 'تاريخ الميلاد',
                                      textInputAction: TextInputAction.next,
                                      textInputType: TextInputType.datetime,
                                      prefix: Container(
                                          margin: EdgeInsets.fromLTRB(
                                              16.h, 12.v, 10.h, 12.v),
                                          child: InkWell(
                                            onTap: (){
                                              cubit.selectDate(context);
                                            },
                                            child: CustomImageView(
                                                imagePath: ImageConstant.icCalendar,
                                                color: Colors.grey,
                                                height: 24.adaptSize,
                                                width: 24.adaptSize),
                                          )),
                                      prefixConstraints: BoxConstraints(maxHeight: 48.v,maxWidth: 48.h),
                                      validator: (value) {
                                        if (value != null && value.isNotEmpty && !isValidDate(value)) {
                                          return 'الرجاء ادخال تاريخ ميلاد صالح(سنة-شهر-يوم)';
                                        }
                                        return null;
                                      },
                                      obscureText: false,
                                      contentPadding: EdgeInsets.only(
                                          top: 15.v, right: 30.h, bottom: 15.v)),
                                  SizedBox(height: 10.v),

                                  // Password
                                  CustomTextFormField(
                                      controller: cubit.passwordController,
                                      focusNode:cubit.passwordFocusNode,
                                      onFieldSubmitted: (value){
                                        FocusScope.of(context).unfocus();
                                        FocusScope.of(context).requestFocus(cubit.phoneFocusNode);
                                      },
                                      hintText: "ادخل كلمة السر",
                                      title: 'كلمة السر',
                                      textInputAction: TextInputAction.next,
                                      textInputType: TextInputType.visiblePassword,
                                      prefix: Container(
                                          margin: EdgeInsets.fromLTRB(
                                              16.h, 12.v, 10.h, 12.v),
                                          child: CustomImageView(
                                              imagePath: ImageConstant.imgLock,
                                              height: 24.adaptSize,
                                              width: 24.adaptSize)),
                                      prefixConstraints: BoxConstraints(maxHeight: 48.v),
                                      validator: (value) {
                                        if(value!= null && value.isNotEmpty && (value.length < 8 || value.length>50) ) {
                                          return "يجب ان تكون كلمة السر اكبر من 8 محارف واقل من 50 محرف".tr;
                                        }
                                        return null;
                                      },
                                      obscureText: false,
                                      contentPadding: EdgeInsets.only(
                                          top: 15.v, right: 30.h, bottom: 15.v)),
                                  SizedBox(height: 10.v),
                                  // Phone
                                  CustomTextFormField(
                                      controller: cubit.phoneController,
                                      focusNode:cubit.phoneFocusNode,
                                      onFieldSubmitted: (value){
                                        FocusScope.of(context).unfocus();
                                        FocusScope.of(context).requestFocus(cubit.rateFocusNode);
                                      },
                                      hintText: "ادخل رقم الهاتف",
                                      title: 'رقم الجوال',
                                      textInputAction: TextInputAction.next,
                                      textInputType: TextInputType.phone,
                                      prefix: Container(
                                          margin: EdgeInsets.fromLTRB(
                                              16.h, 12.v, 10.h, 12.v),
                                          child: CustomImageView(
                                              imagePath: ImageConstant.imgMail,
                                              height: 24.adaptSize,
                                              width: 24.adaptSize)),
                                      prefixConstraints: BoxConstraints(maxHeight: 48.v),
                                      validator: (value) {
                                        if(value == null ) {
                                          //return "err_msg_please_enter_valid_password".tr;
                                        }
                                        return null;
                                      },

                                      contentPadding: EdgeInsets.only(
                                          top: 15.v, right: 30.h, bottom: 15.v)),
                                  SizedBox(height: 10.v),
                                  //First Rate Rate
                                  CustomTextFormField(
                                      controller: cubit.rateController,
                                      focusNode:cubit.rateFocusNode,
                                      onFieldSubmitted: (value){
                                        FocusScope.of(context).unfocus();
                                      },
                                      hintText: "ادخل التقييم الاولي",
                                      title: 'التقييم الاولي',
                                      textInputAction: TextInputAction.done,
                                      textInputType: TextInputType.number,
                                      prefix: Container(
                                          margin: EdgeInsets.fromLTRB(
                                              16.h, 12.v, 10.h, 12.v),
                                          child: CustomImageView(
                                              imagePath: ImageConstant.icLevel,
                                              height: 24.adaptSize,
                                              width: 24.adaptSize)),
                                      prefixConstraints: BoxConstraints(maxHeight: 48.v),
                                      validator: (value) {
                                        if(value == null ) {
                                          //return "err_msg_please_enter_valid_password".tr;
                                        }
                                        return null;
                                      },

                                      contentPadding: EdgeInsets.only(
                                          top: 15.v, right: 30.h, bottom: 15.v)),
                                  /*// Email
                                  CustomTextFormField(
                                      controller: cubit.emailController,
                                      focusNode:cubit.emailFocusNode,
                                      onFieldSubmitted: (value){
                                        FocusScope.of(context).unfocus();
                                        //FocusScope.of(context).requestFocus(cubit.levelFocusNode);
                                      },
                                      hintText: "ادخل البريد الالكتروني",
                                      title: 'البريد الالكتروني',
                                      textInputAction: TextInputAction.next,
                                      textInputType: TextInputType.emailAddress,
                                      prefix: Container(
                                          margin: EdgeInsets.fromLTRB(
                                              16.h, 12.v, 10.h, 12.v),
                                          child: CustomImageView(
                                              imagePath: ImageConstant.imgMail,
                                              height: 24.adaptSize,
                                              width: 24.adaptSize)),
                                      prefixConstraints: BoxConstraints(maxHeight: 48.v),
                                      validator: (value) {
                                        if(value!= null && value.isNotEmpty ) {
                                          //return "err_msg_please_enter_valid_password".tr;
                                        }
                                        return null;
                                      },

                                      contentPadding: EdgeInsets.only(
                                          top: 15.v, right: 30.h, bottom: 15.v)),
                                  SizedBox(height: 10.v),*/
                                ],
                              ),
                          ),
                        )),
                  ),
                  Row(
                    children: [
                      Expanded(
                        flex: 2,
                        child: CustomElevatedButton(
                            height: 45,
                            text:cubit.editMode ?"حفظ" : "إضافة",
                            buttonTextStyle: CustomTextStyles.titleSmallOnPrimaryContainer,
                            margin:const EdgeInsets.all(16),
                            onPressed: () {
                              FocusScope.of(context).unfocus();
                              if(cubit.formKey.currentState!.validate()){
                                if(cubit.editMode) {
                                  cubit.updateUser(context);
                                } else {
                                  cubit.addUser(context);
                                }
                              }
                              //onTapSignIn(context);
                            }),
                      ),
                      Expanded(
                        flex: 2,
                        child: CustomElevatedButton(
                            height: 45,
                            text:"رجوع",
                            buttonStyle: ButtonStyle(
                            backgroundColor: MaterialStateProperty.all<Color>(Colors.redAccent), // Set the desired background color here
                            ),
                            margin:const EdgeInsets.all(16),
                            buttonTextStyle: CustomTextStyles.titleSmallOnPrimaryContainer,
                            onPressed: () {
                              Navigator.pop(context);
                            }),
                      ),
                    ],
                  ),

                ],
              ),


            );
          },
        ),
      ),
    );
  }
  PreferredSizeWidget _buildAppBar(BuildContext context) {
    return CustomAppBar(
        leading: OutlineIconButton(
          imagePath: ImageConstant.imgRightIcon,
          onTapped: (){
            Navigator.pop(context);
          },
        ),
        title: AppbarSubtitle(
            text: "إضافة مدرب", margin: EdgeInsets.only(left: 12.h)));
  }
}
